using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QLHXNK.DataAccess;
using System.Windows.Forms;

namespace QLHXNK.BusinessLogic
{
	class User
	{
		#region Private Members

		private int _userID;
		private String _userName;
		private String _password;
	
		#endregion
	
		#region Properties

		public int UserID
		{
			get { return _userID; }
			set { _userID = value; }
		}

		public String UserName
		{
			get { return _userName; }
			set { _userName = value; }
		}

		public String Password
		{
			get { return _password; }
			set { _password = value; }
		}


		#endregion

		#region Initialization

		public User(int userID, String userName, String password)
		{
			_userID = userID;
			_userName = userName;
			_password = password;
		}

		#endregion

		#region New/Delete/Modify/Save

		// New
		public static User NewUser(String userName, String password)
		{
			try
			{
				int userID = UserData.NewUser(userName, password);
				User user = null;

				if (userID != 0)
				{
					user = new User(userID, userName, password);
				}
				return user;
			}
			catch 
			{
				
				return null;
			}
		}

		// Delete
		public int DeleteUser()
		{
			try
			{
				return UserData.DeleteUser(_userID);
			}
			catch 
			{
				
				return 0;
			}
		}

		// Modify
		public int UpdateUser(String userName, String password)
		{
			try
			{
				_userName = userName;
				_password = password;

				return Save();
			}
			catch 
			{
				
				return 0;
			}
		}

		// Save
		public int Save()
		{
			try
			{
				return UserData.UpdateUser(_userID, _userName, _password);
			}
			catch 
			{
				
				return 0;
			}
		}

		#endregion

		#region Extract Data
		
		public static DataTable ListAll()
		{
            try
            {
                return UserData.GetUsers("", "");
            }
            catch 
            {
                
                return null;
            }
		}

        public static DataTable GetUser(String userName, String password)
        {
            try
            {
                return UserData.GetUsers(userName, password);
            }
            catch 
            {
                
                return null;
            }
        }
		#endregion
	}
}
